package com.cn.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 动态代理类
 */
public class JDKProxy implements InvocationHandler {

    // 动态代理对象
    private Object object;

    public Object newInstance(Object o){
        System.out.println("代理对象构造函数");
        this.object = o;
        Object proxyInstance = Proxy.newProxyInstance(o.getClass().getClassLoader(), o.getClass().getInterfaces(), this);
        return proxyInstance;
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("前置增强");
        Object invoke = method.invoke(object, args);
        System.out.println("后置增强");
        return invoke;
    }
}
